package com.kensure.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.PageInfo;

import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.cj.model.CJBaoming;
import com.kensure.shike.cj.model.CJGuige;
import com.kensure.shike.cj.model.CJTitle;
import com.kensure.shike.cj.service.CJBaomingService;
import com.kensure.shike.cj.service.CJGuigeService;
import com.kensure.shike.cj.service.CJTitleService;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;

/**
 * 抽奖服务
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "cj")
public class CJController {

	@Resource
	private CJTitleService cJTitleService;
	@Resource
	private CJBaomingService cJBaomingService;
	@Resource
	private CJGuigeService cJGuigeService;
	@Resource
	private SKUserService sKUserService;

	/**
	 * 抽奖
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "cjlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo cjlist(HttpServletRequest req, HttpServletResponse rep) {
		List<CJTitle> list = cJTitleService.selectByStart();
		return new ResultRowsInfo(list);
	}

	/**
	 * 规格列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "gglist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo gglist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long cjid = json.getLong("cjid");
		List<CJGuige> list = cJGuigeService.selectGuiGe(cjid);
		return new ResultRowsInfo(list);
	}

	/**
	 * 首页抽奖
	 */
	@ResponseBody
	@RequestMapping(value = "sysj.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo sysj(HttpServletRequest req, HttpServletResponse rep) {
		SKUser user = sKUserService.getUser();
		CJTitle cjtitle = cJTitleService.selectIndex(user);
		return new ResultRowInfo(cjtitle);
	}

	/**
	 * 是否报名了
	 */
	@ResponseBody
	@RequestMapping(value = "isbaoming.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo isbaoming(HttpServletRequest req, HttpServletResponse rep) {
		SKUser user = sKUserService.getUser();
		CJTitle cjtitle = cJTitleService.selectChouJiang(user);
		return new ResultRowInfo(cjtitle);
	}

	/**
	 * 后台信息列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "alllist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo alllist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<CJTitle> list = cJTitleService.selectByWhere(page);
		Long count = cJTitleService.selectCountByWhere(null);
		return new ResultRowsInfo(list, count);
	}

	/**
	 * 信息内容
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "get.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo get(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		CJTitle cms = cJTitleService.selectOne(id);
		return new ResultRowInfo(cms);
	}

	/**
	 * 修改状态
	 */
	@ResponseBody
	@RequestMapping(value = "update.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo update(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		Integer status = json.getInteger("status");
		cJTitleService.updateStatus(id, status);
		return new ResultRowInfo();
	}

	/**
	 * 报名
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "baoming.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo baoming(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		SKUser user = sKUserService.getUser();
		cJBaomingService.add(user, id);
		return new ResultRowInfo();
	}

	/**
	 * 报名人员有效人员
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bmlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo bmlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		List<CJBaoming> list = cJBaomingService.selectByStatus(0, id);
		return new ResultRowsInfo(list);
	}

	/**
	 * 中奖，后续废弃
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "zhongjiang.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo zhongjiang(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long guigeid = json.getLong("guigeid");
		Long userid = json.getLong("userid");
		cJBaomingService.zhongjiang(userid, guigeid);
		return new ResultRowInfo();
	}

	/**
	 * 抽奖
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "choujiang.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo choujiang(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long guigeid = json.getLong("guigeid");
		CJBaoming rpw = cJBaomingService.choujiang(guigeid);
		return new ResultRowInfo(rpw);
	}
}
